package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevMainBaseDao;
import com.sugon.dao.DevMaintenancePlanDao;
import com.sugon.dao.DevMaintenancePlanPreviewDao;
import com.sugon.entity.DevMainBaseEntity;
import com.sugon.entity.DevMaintenancePlanItemEntity;
import com.sugon.entity.DevMaintenancePlanPreviewEntity;
import com.sugon.modules.device.consts.DevMaintenancePlanFreType;
import com.sugon.modules.device.model.entity.DevMaintenancePlanEntity;
import com.sugon.modules.utils.DateUtil;
import com.sugon.service.DevMaintenancePlanPreviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 设备维护计划预览明细表Service实现类
 *
 * @author sugon
 * @date 2020-11-04 15:58:36
 */
@Service("devMaintenancePlanPreviewService")
public class DevMaintenancePlanPreviewServiceImpl implements DevMaintenancePlanPreviewService {

    @Autowired
    private DevMaintenancePlanPreviewDao devMaintenancePlanPreviewDao;
    @Resource
    private DevMaintenancePlanDao devMaintenancePlanDao;
    @Resource
    private DevMainBaseDao mainBaseDao;

    /**
     * recordDeviceMaintenancePlanExeItem
     *
     * @param planId        计划ID
     * @param planItemId    计划明细ID
     * @param devId         设备ID
     * @param devMainItemId 设备维护项ID
     * @param yearly        年度
     * @param weeksOfYear   年度第多少周
     * @return
     */
    @Override
    public boolean recordDeviceMaintenancePlanExeItem(String planId, String planItemId, String devId, String devMainItemId, int yearly, int weeksOfYear) {
        cn.hutool.core.lang.Assert.notBlank(devId, "生成设备维护计划实施项时，设备ID必填！");
        cn.hutool.core.lang.Assert.notBlank(devMainItemId, "生成设备维护计划实施项时，设备维护项ID必填！");
        return save(new DevMaintenancePlanPreviewEntity(planId, planItemId, devId, devMainItemId, yearly, weeksOfYear));
    }

    @Override
    public void rebuild(List<DevMaintenancePlanItemEntity> planItemList) {

        for (DevMaintenancePlanItemEntity item : planItemList) {
            // 删除现有的预览数据
            devMaintenancePlanPreviewDao.deleteByPlanId(item.getPlanId(), item.getDevId(), CollectionUtil.newArrayList(item.getMainBaseId()), null);
            // 生成新的
            DevMaintenancePlanEntity plan = devMaintenancePlanDao.queryObject(item.getPlanId());
            Assert.notNull(plan);
            DevMainBaseEntity mainBase = mainBaseDao.queryObject(item.getMainBaseId());
            Assert.notNull(mainBase);
            Date startDay = DateUtil.initDate(plan.getYearly(), item.getMonthNum(), item.getDayNum());
            // 生成记录
            recursiveSetExePoint(startDay, mainBase.getFreRate(), plan.getStartDate(), plan.getEndDate(), true, item.getPlanId(), item.getDevId(), item.getMainBaseId());
        }
    }

    /**
     * 递归循环设置计划实施点
     *
     * @param date
     * @param freRate
     * @param compareStartDate
     * @param compareEndDate
     * @param isFirst
     * @param planId
     * @param devId
     * @param mainBaseId
     */
    private void recursiveSetExePoint(Date date, Integer freRate, Date compareStartDate, Date compareEndDate, boolean isFirst, String planId, String devId, String mainBaseId) {
        if (!isFirst) {
            // 根据设备维护计划实施频率对date进行偏移
            switch (freRate) {
                case DevMaintenancePlanFreType.WEEKLY:
                    // 每周执行一次
                    date = cn.hutool.core.date.DateUtil.offsetWeek(date, 1);
                    break;
                case DevMaintenancePlanFreType.FORTNIGHT:
                    // 每半个月执行一次
                    date = cn.hutool.core.date.DateUtil.offsetWeek(date, 2);
                    break;
                case DevMaintenancePlanFreType.MONTH:
                    // 每月执行一次
                    date = cn.hutool.core.date.DateUtil.offsetMonth(date, 1);
                    break;
                case DevMaintenancePlanFreType.QUARTER:
                    // 每季度执行一次
                    date = cn.hutool.core.date.DateUtil.offsetMonth(date, 3);
                    break;
                case DevMaintenancePlanFreType.HALF_YEAR:
                    // 每半年执行一次
                    date = cn.hutool.core.date.DateUtil.offsetMonth(date, 6);
                    break;
                case DevMaintenancePlanFreType.YEAR:
                    // 每年执行一次
                    date = cn.hutool.core.date.DateUtil.offsetMonth(date, 12);
                    break;
            }
        }
        // 如果计划不再实施起止时间段内，那么不做任何处理
        //if (date.compareTo(compareEndDate) > 0 || date.compareTo(compareStartDate) < 0) {
        //    return;
        //}
        if (date.compareTo(compareEndDate) > 0) {
            return;
        }
        if (DateUtil.between(date, compareStartDate, compareEndDate)) {
            int weekOfYear = cn.hutool.core.date.DateUtil.weekOfYear(date);
            // 如果不是第一次且weekOfYear为1,那么计算当前日期的前一周的周次+1，修复无法显示最后一周BUG
            if (!isFirst && weekOfYear == 1) {
                weekOfYear = cn.hutool.core.date.DateUtil.weekOfYear(cn.hutool.core.date.DateUtil.offsetWeek(date, -1)) + 1;
            }
            recordDeviceMaintenancePlanExeItem(planId, null, devId, mainBaseId, cn.hutool.core.date.DateUtil.year(date), weekOfYear);
        }
        recursiveSetExePoint(date, freRate, compareStartDate, compareEndDate, false, planId, devId, mainBaseId);
    }

    /**
     * save
     *
     * @param devMaintenancePlanPreview
     * @return
     */
    @Override
    public boolean save(DevMaintenancePlanPreviewEntity devMaintenancePlanPreview) {
        devMaintenancePlanPreview.setId(RandomUtil.randomString(32));
        return devMaintenancePlanPreviewDao.save(devMaintenancePlanPreview) > 0;
    }

    /**
     * delete
     *
     * @param yearly
     */
    @Override
    public void delete(String yearly) {
        devMaintenancePlanPreviewDao.delete(Integer.parseInt(yearly));
    }
}
